package com.syn.synold4s.repository;

import static com.syn.synold4s.constants.TagCode.API_KEY;

import com.gaoyi.basemodule.apiresult.ApiResultData;
import com.gaoyi.basemodule.baseCallback.Callback;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.GsonUtil;
import com.gaoyi.basemodule.baseUtil.OKHttpUtil;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.basebean.LoginDto;
import com.gaoyi.basemodule.basebean.ProjectDto;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.google.gson.reflect.TypeToken;
import com.syn.synold4s.bean.dto.AssetDto;
import com.syn.synold4s.bean.dto.GasMonitoringCountDto;
import com.syn.synold4s.bean.dto.GassDto;
import com.syn.synold4s.bean.dto.MainDto;
import com.syn.synold4s.bean.dto.MapAllDeviceDto;
import com.syn.synold4s.bean.dto.PermitToWorkDto;
import com.syn.synold4s.bean.dto.SafetyTrainingDto;
import com.syn.synold4s.bean.dto.UnSafeActDto;
import com.syn.synold4s.bean.dto.UnSafeActsDto;
import com.syn.synold4s.bean.dto.WatchListDto;

import java.lang.reflect.Type;
import java.util.List;

public class MainRepository {
    public List<ProjectDto> getProjects() {
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/utils/combos?typeKey=projs");
        if (result == null) {
            return null;
        }
        ApiResultData<List<ProjectDto>> apiResultData = (ApiResultData<List<ProjectDto>>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<List<ProjectDto>>>() {
        }.getType());
        return apiResultData.getData();
    }

    public MainDto getData(String projCode, String synProj, String keyWord) {
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/Dashboards?projCode=" + projCode + "&synProj=" + synProj + "&keyWord=" + keyWord);
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<MainDto> apiResultData = (ApiResultData<MainDto>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<MainDto>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public LoginDto getTagByProjId(String id) {
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/projs/tags?projId=" + id);
        if (result == null) {
            return null;
        }
        ApiResultData<LoginDto> apiResultData = (ApiResultData<LoginDto>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<LoginDto>>() {
        }.getType());
        return apiResultData.getData();
    }

    public void changeFilter(Object bean, OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/users/updateProjFilter", bean, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                onResultListener.onFails(e);
            }
        }, typeofT);
    }

    public List<MapAllDeviceDto> allMapDevices(String tagCode) {
        String projCode = SPUtil.getInstance().getString(SPName.PROJ_CODE);
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/Dashboards/GetAllDevice?projCode="+projCode+"&tagCode="+tagCode);
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<List<MapAllDeviceDto>> apiResultData = (ApiResultData<List<MapAllDeviceDto>>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<List<MapAllDeviceDto>>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public UnSafeActDto unsafeAct(){
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/UACT/dashboards");
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<UnSafeActDto> apiResultData = (ApiResultData<UnSafeActDto>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<UnSafeActDto>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public SafetyTrainingDto safetyTraining(){
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/VRST/Dashboards");
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<SafetyTrainingDto> apiResultData = (ApiResultData<SafetyTrainingDto>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<SafetyTrainingDto>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public AssetDto trackingSystem(){
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/ts/dashboards");
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<AssetDto> apiResultData = (ApiResultData<AssetDto>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<AssetDto>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public WatchListDto deviceById(String tagCode, String id){
        String projCode = SPUtil.getInstance().getString(SPName.PROJ_CODE);
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/Dashboards/Details?projCode="+projCode+"&tagCode="+tagCode+"&id="+id);
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<WatchListDto> apiResultData = (ApiResultData<WatchListDto>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<WatchListDto>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<PermitToWorkDto> permitToWorkList(String code){
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/PTWST/dashboard/getOrderApproveStateToIOS?code="+code);
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<List<PermitToWorkDto>> apiResultData = (ApiResultData<List<PermitToWorkDto>>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<List<PermitToWorkDto>>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public GasMonitoringCountDto gasMonitoringCount(){
        String result = OKHttpUtil.getInstance().getSync(OKHttpUtil.NET_URL + "/api/HGMST/dashboards/GetEntranceAll");
        if (result == null) {
            return null;
        }
        try {
            ApiResultData<GasMonitoringCountDto> apiResultData = (ApiResultData<GasMonitoringCountDto>) GsonUtil.getInstance().fromJson(result, new TypeToken<ApiResultData<GasMonitoringCountDto>>() {
            }.getType());
            return apiResultData.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void reader_360(OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().postAsync(OKHttpUtil.BASE_NET_URL + "/api/MPST/dashboard/getReader", "", new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                onResultListener.onFails(e);
            }
        }, typeofT);
    }

    public void device_360(Object bean, OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().postAsync(OKHttpUtil.BASE_NET_URL + "/api/MPST/dashboard", bean, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                onResultListener.onFails(e);
            }
        }, typeofT);
    }
}
